/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.sys.service;

import com.thinkgem.jeesite.common.persistence.DataEntity;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.modules.sys.dao.*;
import com.thinkgem.jeesite.modules.sys.entity.*;
import com.thinkgem.jeesite.modules.sys.utils.Configuration;
import com.thinkgem.jeesite.modules.sys.utils.NumberGenerator;
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;
import org.omg.PortableInterceptor.INACTIVE;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 店铺Service
 * @author ThinkGem
 * @version 2014-05-16
 */
@Service
@Transactional(readOnly = true)
public class AgentTenementService {
	@Autowired
	private AgentTenementDao agentTenementDao;
	@Autowired
	private ShopAddDao shopAddDao;
	@Autowired
	private AddTenementInfoDao addTenementInfoDao;
	@Autowired
	private TbOrderMapper tbOrderMapper;
	@Autowired
	private TbShopProductDao tbShopProductDao;
	@Autowired
	private UserDao userDao;

	public Page<TbUserTenementInfo> agentTenementList(Page<TbUserTenementInfo> page,TbUserTenementInfo tbUserTenementInfo){
		String uid=UserUtils.getUser().getId();
		User users=shopAddDao.userAreas(uid);
		tbUserTenementInfo.setProvince(Integer.parseInt(users.getProvinces()));
		tbUserTenementInfo.setCity(Integer.parseInt(users.getCitys()));
		Date date=tbUserTenementInfo.getEndDate();
		if(date!=null){
			date.setHours(23);
			date.setMinutes(59);
			date.setSeconds(59);
		}
		tbUserTenementInfo.setPage(page);
		page.setList(agentTenementDao.agentTenementList(tbUserTenementInfo));
		return page;
	}

	//代理商到处信息时信息时的模板操作
	public Page<TbUserTenementInfo> exportTenement(Page<TbUserTenementInfo> page,TbUserTenementInfo tbUserTenementInfo){
		String uid=UserUtils.getUser().getId();
		User users=shopAddDao.userAreas(uid);
		tbUserTenementInfo.setProvince(Integer.parseInt(users.getProvinces()));
		tbUserTenementInfo.setCity(Integer.parseInt(users.getCitys()));
		tbUserTenementInfo.setPage(page);
		List list=agentTenementDao.agentTenementList(tbUserTenementInfo);
		for(int i=0;i<list.size();i++){
			TbUserTenementInfo tt=(TbUserTenementInfo)list.get(i);
			tt.setCoefficient(new BigDecimal(Double.toString(tt.getBuildingNo())));
		}
		page.setList(list);
		return page;
	}
	//物业管理员的物业信息
	public Page<TbUserTenementInfo> manageTenementList(Page<TbUserTenementInfo> page,TbUserTenementInfo tbUserTenementInfo){
		User user=UserUtils.getUser();
		User users=shopAddDao.userAreas(user.getId());
		//得到物业管理员所在的省市区
		tbUserTenementInfo.setProvince(Integer.parseInt(users.getProvinces()));
		tbUserTenementInfo.setCity(Integer.parseInt(users.getCitys()));
		tbUserTenementInfo.setAreas(Integer.parseInt(users.getAreas()));
		//登陆信息中的小区id
		tbUserTenementInfo.setCommunityId(Integer.parseInt(user.getCommunityId()));
		tbUserTenementInfo.setPage(page);
		Date date=tbUserTenementInfo.getEndDate();
		if(date!=null){
			date.setHours(23);
			date.setMinutes(59);
			date.setSeconds(59);
		}
		List list=agentTenementDao.manageTenementList(tbUserTenementInfo);
		page.setList(list);
		return page;
	}
	//物业管理员到处物业信息
	public Page<TbUserTenementInfo> manageExportTenementList(Page<TbUserTenementInfo> page,TbUserTenementInfo tbUserTenementInfo){
		User user=UserUtils.getUser();
		User users=shopAddDao.userAreas(user.getId());
		//得到物业管理员所在的省市区
		tbUserTenementInfo.setProvince(Integer.parseInt(users.getProvinces()));
		tbUserTenementInfo.setCity(Integer.parseInt(users.getCitys()));
		tbUserTenementInfo.setAreas(Integer.parseInt(users.getAreas()));
		//登陆信息中的小区id
		tbUserTenementInfo.setCommunityId(Integer.parseInt(user.getCommunityId()));
		tbUserTenementInfo.setPage(page);
		List list=agentTenementDao.manageTenementList(tbUserTenementInfo);
		for(int i=0;i<list.size();i++){
			TbUserTenementInfo tt=(TbUserTenementInfo)list.get(i);
			tt.setCoefficient(new BigDecimal(Double.toString(tt.getBuildingNo())));
		}
		page.setList(list);
		return page;
	}

	public TbUserTenementInfo getByMobile(String mobile){
		return agentTenementDao.getByMobile(mobile);
	}
	//得到楼号集合
	public List<TbUserTenementInfo> getBuildNumList(){
		User user=UserUtils.getUser();
		User us=userDao.queryUser(user.getId());
		String communityId=us.getCommunityId();
		TbUserTenementInfo tbUserTenementInfo=new TbUserTenementInfo();
		if(!"".equals(communityId)&&null!=communityId){
			tbUserTenementInfo.setCommunityId(Integer.parseInt(communityId));
			return agentTenementDao.getBuildNumList(tbUserTenementInfo);
		}else{
			List<TbUserTenementInfo> str=new ArrayList<TbUserTenementInfo>();
			tbUserTenementInfo.setIsok("true");
			str.add(tbUserTenementInfo);
			return str;
		}


	}
	//根据楼号得到单元号集合
	public List<TbUserTenementInfo> getUnitNumList(String buildNum){
		User user=UserUtils.getUser();
		String communityId=user.getCommunityId();
		TbUserTenementInfo tbUserTenementInfo=new TbUserTenementInfo();
		tbUserTenementInfo.setCommunityId(Integer.parseInt(communityId));
		tbUserTenementInfo.setBuildNum(buildNum);
		return agentTenementDao.getBuildNumList(tbUserTenementInfo);
	}
	//根据单元号得到门牌号
	public List getDoorNoList(String buildNum,Integer uinitNo){
		User user=UserUtils.getUser();
		String communityId=user.getCommunityId();
		TbUserTenementInfo tbUserTenementInfo=new TbUserTenementInfo();
		tbUserTenementInfo.setCommunityId(Integer.parseInt(communityId));
		tbUserTenementInfo.setBuildNum(buildNum);
		tbUserTenementInfo.setUnitNo(uinitNo);
		return agentTenementDao.getBuildNumList(tbUserTenementInfo);
	}
	//根据门牌号得到物业信息
	public  List<TbUserTenementInfo>  getTenementInfo(String buildNum,Integer uinitNo,String showName){
		User user=UserUtils.getUser();
		String communityId=user.getCommunityId();
		TbUserTenementInfo tbUserTenementInfo=new TbUserTenementInfo();
		tbUserTenementInfo.setCommunityId(Integer.parseInt(communityId));
		tbUserTenementInfo.setBuildNum(buildNum);
		tbUserTenementInfo.setUnitNo(uinitNo);
		tbUserTenementInfo.setShowName(showName);
		return agentTenementDao.getBuildNumList(tbUserTenementInfo);
	}
	//线下物业缴费
	@Transactional(readOnly = false)
	public String tenementMoney(TbUserTenementInfo tbUserTenementInfo){
		User user=UserUtils.getUser();
		TbUserTenementInfo tt=addTenementInfoDao.getById(tbUserTenementInfo);
		//根据uid得到roid，如果是商户，则使用id得到店铺id，如果是收银员，则用shop_id得到店铺id
		TbOrder tbOrder=new TbOrder();
		Date date=new Date();
		tbOrder.setUid("");
		tbOrder.setPlatFormType(5);
		tbOrder.setChannelId(0);
		tbOrder.setContactName(tt.getName());
		tbOrder.setContactMobile(tt.getMobile());
		tbOrder.setOrderAmount(tbUserTenementInfo.getMoney());
		tbOrder.setPayName("现金");
		tbOrder.setPayAmount(tbUserTenementInfo.getMoney());
		tbOrder.setCreateTime(date);
		tbOrder.setUpdateTime(date);
		tbOrder.setPayNote("线下物业缴费");
		tbOrder.setOrderDescribe("线下物业缴费");
		tbOrder.setAddressId(tt.getCommunityId());
		tbOrder.setMchId(1);//固定传1
		tbOrder.setRefundReason("");
		tbOrder.setRefundState(new Byte("0"));
		tbOrder.setRefundNotifyUrl("");
		tbOrder.setRefundIngAmount(new BigDecimal("0"));
		tbOrder.setOrderSubject("线下物业缴费");
		tbOrder.setExceptionRefundNotify("");
		tbOrder.setRefundNotifyUrl("");
		tbOrder.setCloseNotifyUrl("");
		tbOrder.setOrderUrl("");
		tbOrder.setShowUrl("");
		tbOrder.setExtraParam("");
		tbOrder.setService("");
		tbOrder.setBeneficiaryName("");
		tbOrder.setProductType("");
		String orderno=NumberGenerator.getNextStreamNo();
		tbOrder.setOrderNo(orderno);
		tbOrder.setOrderState((byte)1);
		tbOrder.setPayState((byte)1);
		tbOrder.setPayId((byte)1);
		tbOrder.setNotifyUrl(""+ Configuration.getInstance().getStringValue("backUrl.url","")
				+"/AgentProduct/updateAgentProduct?orderNo="
				+tbOrder.getOrderNo()+"&agentId="+user.getId()+"");
		tbOrder.setIsDelete(new Byte("0"));
		String url = Configuration.getInstance().getStringValue("backUrl.url","");
		tbOrder.setOrderUrl(""+url+"/bbc/order/pay?agentId="+user.getId()+"");
		tbOrderMapper.insertTbOrder(tbOrder);
		//插入物业缴费历史
		TbUserTenementInfo ts=new TbUserTenementInfo();
		ts.setId(tt.getId());
		ts.setUid(orderno);
		ts.setCreateTime(new Date());
		ts.setUpdateTime(new Date());
		ts.setMoney(tbUserTenementInfo.getMoney());
		ts.setMonth(tbUserTenementInfo.getMonth());
		ts.setIsActive(tbUserTenementInfo.getIsActive());
		agentTenementDao.tenemMoney(ts);
		//物业钱包历史记录
		if(tbUserTenementInfo.getIsActive().equals("1")){//参与了物业缴费活动
			String mobile=tbUserTenementInfo.getMobile();
			TbUser tbUser=tbShopProductDao.getTbUserByPhone(mobile);
			if(null!=tbUser){
				tbUser.setMqtotalMoney(tbUserTenementInfo.getMoney());
				//物业钱包充值
				tbShopProductDao.insetTenementWallet(tbUser);
				//物业钱包流水
				TbTenementWalletStream tbTenementWalletStream=new TbTenementWalletStream();
				tbTenementWalletStream.setUid(tbUser.getUid());
				tbTenementWalletStream.setType("0");
				tbTenementWalletStream.setCategory("9");//物业费
				tbTenementWalletStream.setChangeMoney(tbUser.getMqtotalMoney());

				tbTenementWalletStream.setIsDelete(new Byte("0"));
				tbTenementWalletStream.setCreateTime(date);
				tbTenementWalletStream.setUpdateTime(date);
				tbTenementWalletStream.setPayType("4");//现金充值
				tbShopProductDao.inserTenementStream(tbTenementWalletStream);

				return "true";
			}else{
				return "该手机号没有关注麻雀家急账号！";
			}
		}
		return "true";
	}

}
